import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  // 定义状态
  state: {
    userName: '', // 用户名
    userHeadImg: '', // 用户头像
    isLogin: '' // 登录状态
  },
  mutations: {
    // 用户名
    userName (state, name) {
      state.userName = name
      localStorage.setItem('userName', name)
    },
    // 头像
    userHeadImg (state, img) {
      state.userHeadImg = img
      localStorage.setItem('userHeadImg', img)
    },
    // 登录状态
    isLogin (state, loginState) {
      state.isLogin = loginState
      localStorage.setItem('isLogin', loginState)
    },
    // 清除
    clear (state) {
      state.userName = ''
      state.userHeadImg = ''
      state.isLogin = 'no'
    }
  }
})

export default store
